/**
 * 
 * @purpose:ELEMENT HAS MOTION..
 * @auth: Ning
 * @date: 2009-8-25
 * 
 * */
package views
{
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	dynamic public class BGElement extends Sprite
	{
		//START...
		
		
		//PROPERTY...
		
		private var background:Sprite;
		
		private var _mouseEnabled:Boolean;
		
		private var _buttonMode:Boolean;
		
				
		//CONSTUCTOR...
		public function BGElement()
		{
			init();
		}
		
		//METHODS...
		
		//initlizing...
		public function init():void
		{
			background = getChildByName("bgFade") as Sprite;
			
			if(background)
			{
				addEventListener(MouseEvent.CLICK,checkTarget,false,1,true);
				addEventListener(MouseEvent.MOUSE_OVER,checkTarget,false,1,true);
				addEventListener(MouseEvent.MOUSE_OUT,checkTarget,false,1,true);
				addEventListener(MouseEvent.MOUSE_MOVE,checkTarget,false,1,true);
				addEventListener(MouseEvent.MOUSE_DOWN,checkTarget,false,1,true);
				addEventListener(MouseEvent.MOUSE_UP,checkTarget,false,1,true);	
			}
		}
		
		
		// click handler....
		private function checkTarget(evt:MouseEvent):void
		{
			
			if(evt.target == this && background != null)
				evt.stopImmediatePropagation();
					
		}
		
		//override get width...
		public override function get width():Number
		{
						
			return background ? background.width * scaleX : super.width;
						
		}
		
		
		//override get height...
		public override function get height():Number
		{
								
			return background ? background.height * scaleY : super.height;
			
		}
		
		// override getbound...
		public override function getBounds(targetCoordinateSpace:DisplayObject):Rectangle
		{
									
			return (background) ? background.getBounds(targetCoordinateSpace) :  super.getBounds(targetCoordinateSpace);
						
		}
		
		//override set mouse children...
		public override function set mouseChildren(enable:Boolean):void
		{
			
			if(background)
			{
				
				_mouseEnabled = enable;
				
				for(var i:int = 0;i < numChildren;i++)
				{
				
					if(getChildAt(i) is InteractiveObject && getChildAt(i) != background)
					{
						
						(getChildAt(i) as InteractiveObject).mouseEnabled = enable;
						
						if(getChildAt(i) is Sprite) 
						(getChildAt(i) as Sprite).mouseChildren = enable;
							
					}
						
										
				}
								
			}else{
				
				super.mouseChildren = enable;
				
			}
						
		}
		
		//get mouse enalbed...
		public override function get mouseChildren():Boolean
		{
			
			return background ? _mouseEnabled : super.mouseChildren ;
			
		}
		
		//set button mode...]
		public override function set buttonMode(value:Boolean):void
		{
			
			if(background)
			{
				
				_buttonMode = value;
				
				background.buttonMode = value;
				
			}else{
				
				
				super.buttonMode = value;
				
			}
			
		}
		
		//get button mode...
		public override function get buttonMode():Boolean
		{
			
			return background ? _buttonMode : super.buttonMode;
			
		}
		
		
		//END...
	}
}